﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using GameSchool.Models.Interfaces;
using GameSchool.Models.ViewModels;
///<summary>
///User Repository
///------------------------------------------
///Used to control every aspect regarding users with CRUD, there are also functions to select users
///from the database and the assignments they have assigned to them.
///
namespace GameSchool.Models.Repositories
{
    public class UserRepository : IUserRepository
    {
          GameSchoolDBDataContext db = new GameSchoolDBDataContext();

          //Add a new user profile
          public void AddUserProfile(UserProfileModel u)
          {
              UserProfile original = new UserProfile();

              original.Name = u.Name;
              original.Mobile = u.Mobile;
              original.SSN = u.SSN;
              original.UserID = u.userID;
              original.DateOfBirth = u.DateOfBirth;
              original.Address = u.Address;

              db.UserProfiles.InsertOnSubmit(original);  
          }

          public void EditUserProfile(UserProfileModel u, string pictureLocation)
          {
              var original = GetUserProfileByID((Guid)Membership.GetUser().ProviderUserKey);
              original.Name = u.Name;
              original.Mobile = u.Mobile;
              original.SSN = u.SSN;
              original.UserID = u.userID;
              original.DateOfBirth = u.DateOfBirth;
              original.Address = u.Address;
              original.Picture = pictureLocation;

              db.SubmitChanges();
             
          }

         //Select one user by his ID
          public UserProfile GetUserProfileByID(Guid? uID)
         {
             //Frá Víðiskóða
            if(uID == null)
                throw new ArgumentNullException("GetUserByID - null passed as parameter");

            var selectedUser = (from n in  db.UserProfiles
                                where n.UserID == uID
                                select n).SingleOrDefault();
             return selectedUser;
         }

         //Gets all users
         public IQueryable<UserProfile> GetUsers()
         {
             var userList = (from n in db.UserProfiles
                            orderby n.Name ascending
                            select n);

             return userList;
         }

        
         //Selects all users from a specific course
         public IQueryable<UserProfile> GetUsersByCourse(int? cID)
         {
             
             var courseUserList = from users in db.UserProfiles
                                  from course in db.IsInCourses
                                  where users.UserID == course.UserID
                                  where course.CourseID == cID
                                  select users;
             return courseUserList;

         }

         public void Save()
         {
             db.SubmitChanges();
         }  
                      
        }

    }


